package part1.five;

/**
 * @Author: shaochong
 * @Date: 2021/8/7
 * @Description: 回文字符串暴力破解法
 */
public class Solution5 {
    public static String longestPalindrome(String s) {
        char[] arr = s.toCharArray();
        int i = 0;
        int j = 0;
        int max = 0;
        int ans1 = 0;
        int ans2 = 0;

        for (; i<arr.length; i++) {
            for (j=i+1; j<arr.length; j++) {
                if (j-i > max && isHuiWen(arr, i, j)) {
                    max = j-i;
                    ans1 = i;
                    ans2 = j;
                }
            }
        }
        return s.substring(ans1, ans2);
    }

    private static boolean isHuiWen(char[] arr, int start, int end) {
        while(end >= start) {
            if(arr[end] == arr[start]) {
                end--;
                start++;
            } else {
                return false;
            }
        }
        return true;
    }
}
